package tests;

import common.Utils;
import org.openqa.selenium.By;

import java.io.IOException;
import java.time.Duration;

public class RegisterPage extends Utils {
    public static String url = "http://120.76.141.143:58080/sign-in.html";
    public static String res = "";

    public RegisterPage() {
        super(url);
    }

    //检查页面是否加载成功
    public  void registPageRight() throws InterruptedException, IOException {
        String res = driver.findElement(By.cssSelector("#signUpForm > div > h2")).getText();
        assert res.equals("用户注册");
    }

    //基础控件测试（含点击按钮，输入信息至输入框
    public void basicTest() throws InterruptedException, IOException {
        //检查用户名框输入
        driver.findElement(By.cssSelector("#username")).sendKeys("111");
        String edit = driver.findElement(By.cssSelector("#username")).getAttribute("value");
        assert edit.equals("111");

        //检查昵称框输入
        driver.findElement(By.cssSelector("#nickname")).sendKeys("11122");
        edit = driver.findElement(By.cssSelector("#nickname")).getAttribute("value");
        assert edit.equals("11122");

        //检查密码框输入
        driver.findElement(By.cssSelector("#password")).sendKeys("1112233");
        edit = driver.findElement(By.cssSelector("#password")).getAttribute("value");
        assert edit.equals("1112233");

        //检查确认密码框输入
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("111223344");
        edit = driver.findElement(By.cssSelector("#passwordRepeat")).getAttribute("value");
        assert edit.equals("111223344");

        //检查密码框隐藏与显示密码
        driver.findElement(By.cssSelector("#password_a > svg")).click();
        res = driver.findElement(By.cssSelector("#password")).getAttribute("type");
        assert res.equals("text");

        driver.findElement(By.cssSelector("#password_a > svg")).click();
        res = driver.findElement(By.cssSelector("#password")).getAttribute("type");
        assert res.equals("password");

        //检查确认密码框隐藏与显示密码
        driver.findElement(By.cssSelector("#passwordRepeat_a > svg")).click();
        res = driver.findElement(By.cssSelector("#passwordRepeat")).getAttribute("type");
        assert res.equals("text");

        driver.findElement(By.cssSelector("#passwordRepeat_a > svg")).click();
        res = driver.findElement(By.cssSelector("#passwordRepeat")).getAttribute("type");
        assert res.equals("password");

        //检查条款勾选与否
        driver.findElement(By.cssSelector("#policy")).click();
        res = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        assert res.equals("form-check-input is-valid");

        driver.findElement(By.cssSelector("#policy")).click();
        res = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        assert res.equals("form-check-input is-invalid");

    }

    //检查用户名重复提示错误
    public void usernameDuplicated() throws InterruptedException, IOException {
        //填写错误信息
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("Morningstar");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("11122");
        driver.findElement(By.cssSelector("#password")).sendKeys("1112233");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("1112233");
        res = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        while (!res.equals("form-check-input is-valid"))
        {
            driver.findElement(By.cssSelector("#policy")).click();
            res = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        }
        driver.findElement(By.cssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        assert res.equals("用户已存在");
    }

    //点击登录按钮进入注册页面
    public void EnterLoginPage(){
        driver.findElement(By.cssSelector("body > div > div > div.text-center.text-muted.mt-3 > a")).click();
        res = driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")).getText();
        assert res.equals("用户登录");
    }
}
